package edu.njfu.sas.dao.impl;

import edu.njfu.sas.dao.UserDao;
import edu.njfu.sas.model.User;
import edu.njfu.sas.util.C3p0Util;
import edu.njfu.sas.util.DBHelper;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class   UserDapImpl implements UserDao {

    @Override
    public boolean chickLogin(User user) {
        String sql="select count(*) as count from tbl_user where username=? and password=?";
        long count=0;
        QueryRunner query=new QueryRunner(C3p0Util.getConnection());
        try{
            Map<String,Object> map=query.query(sql,new MapHandler(),new Object[]{user.getUsername(),user.getPassword()});
            count=(Long)map.get("count");
            return count>0;
        } catch (SQLException throwables) {
            return  false;
        }
    }

    @Override
    public boolean registerUser(User user) {
        String sql="insert into tbl_user (username, password) value (?,?)";
        long count=0;
        QueryRunner query=new QueryRunner(C3p0Util.getConnection());
        try{
          return query.update(sql,new Object[]{user.getUsername(),user.getPassword()})>0;
//            count=(Long)map.get("count");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return false;
    }

    public static void main(String[] args) {
        UserDapImpl userDap=new UserDapImpl();
        System.out.println(userDap.registerUser(new User("sdfsd","sdfsd")));
    }

}
